<template>
  <div class="name-edit-container">
    <!--  导航栏  -->
    <van-nav-bar
      title="设置昵称"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('close')"
      @click-right="onClickRight"
    />
    <!--多文本-->
    <van-field
      class="van-field-style"
      v-model="fieldText"
      rows="2"
      autosize
      type="textarea"
      maxlength="11"
      show-word-limit
    >
    </van-field>
  </div>
</template>

<script>
import { updateUserProfileAPI } from '../../../api/index.js'

export default {
  name: 'NameEdit',
  props: {
    userName: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      fieldText: this.userName
    }
  },
  methods: {
    async onClickRight () {
      try {
        const { data: res } = await updateUserProfileAPI({
          name: this.fieldText
        })
        console.log(res)
        // 更新视图
        this.$emit('update:userName', this.fieldText)
        // 关闭弹框
        this.$emit('close')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang="less">
.name-edit-container {
  background-color: #f5f7f9;

  .van-field-style {
    color: #1b1f23;
  }
}
</style>
